package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;

public class GoogleTalkLoginGui extends JDialog {
	private static final long serialVersionUID = 1L;
	
	private JTextField _usernameInput;
	private JPasswordField _passwordInput;
	
	private JButton _ok;
	
	public GoogleTalkLoginGui(Frame owner, String title, boolean modal) {
		super(owner, title, modal);
		this.setLocation(owner.getLocation());
		
		Border emptyBorder = BorderFactory.createEmptyBorder(2, 2, 2, 2);
		
		JLabel usernameLabel = new JLabel("Username (Google Talk)");
		usernameLabel.setBorder(emptyBorder);
		_usernameInput = new JTextField("Same username as used with Google Talk");
		
		JLabel passwordLabel = new JLabel("Password (Google Talk)");
		passwordLabel.setBorder(emptyBorder);
		_passwordInput = new JPasswordField();
		_passwordInput.addKeyListener(new KeyAdapter(){
			@Override
			public void keyReleased(KeyEvent e) {
				if(e.getKeyCode() == KeyEvent.VK_ENTER){
					e.consume();
					_ok.doClick();
				}
			}
			
		});
		
		JPanel panel = new JPanel(new GridLayout(0,2));
		panel.add(usernameLabel);
		panel.add(_usernameInput);
		panel.add(passwordLabel);
		panel.add(_passwordInput);
		
		this.getContentPane().add(panel, BorderLayout.CENTER);
		
		_ok = new JButton("OK");
		_ok.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e)  {
				if (isInputValid())
					GoogleTalkLoginGui.this.setVisible(false);
			}
		});
		
		this.getContentPane().add(_ok, BorderLayout.SOUTH);
		
		this.setMinimumSize(new Dimension(400,0));
		this.pack();
		this.setVisible(true);
	}
	
	public String getUsername(){
		return _usernameInput.getText().trim();
	}
	
	public String getPassword(){
		return String.valueOf(_passwordInput.getPassword()).trim();
	}
	
	private boolean isInputValid(){
		if(_usernameInput.getText().trim() == "" || String.valueOf(_passwordInput.getPassword()).trim() == "")
			return false;
		else
			return true;
	}
}
